/*

欢迎界面的构造



*/

import UIKit
import SDWebImage
class CZWelcomeVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

     setupUI()
    }
    
    override func viewDidAppear(animated: Bool) {
        
        
        //执行动画
        
        super.viewDidAppear(animated)
        
        
        headImageView.snp_updateConstraints { (make) -> Void in
            
            make.top.equalTo(100)
            
        }
        
        
        /*
        
        参数的含义
        
        SpringWithDamping 阻尼
        
        initialSpringVelocity 初始化速度
        
        options 动画选项,一般的枚举我们都是用[],这个要注意
        
        
        
        
        
        
        */
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
//上面的动画完成之后执行的代码
                
                UIView .animateWithDuration(1, animations: { () -> Void in
                    
                    self.nameLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        print("执行动画完毕,切换控制器")
                        
//                    let appDelegate =  UIApplication.sharedApplication().delegate as? AppDelegate
//                        
//                        appDelegate?.window?.rootViewController = CZMainViewController()
//                        
                        
                        //动画完成切换主界面
                        NSNotificationCenter.defaultCenter().postNotificationName(CZChangeRootVCNotification, object: self)
                        
                        
                        
                        //跳转界面,发送通知,将接受通知的
                })
                
                
                
                
        }
        
        
        
        
        
        
        
        
    }
    
    
    
    private func setupUI(){
        
        view.backgroundColor = UIColor(white: 247/255, alpha: 1)
        
        
        //设置UI界面
        
        view.addSubview(headImageView)
        
        view.addSubview(nameLabel)
        
        //设置约束
        
        headImageView.snp_makeConstraints { (make) -> Void in
            
            
            make.centerX.equalTo(view)
            
            make.size.equalTo(CGSize(width: 80, height: 80))
            
            make.top.equalTo(200)
            
            
            
        }
        
        //设置name的约束
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            //距离上面的label多少距离
            
            make.top.equalTo(headImageView.snp_bottom).offset(20)
            
            make.centerX.equalTo(headImageView.snp_centerX)
            
            
        }
        
        //如何执行动画,以及什么时候执行动画
        
        //执行动画一定要在显示好,在进行执行,这样才能显示出来
        
        
    
    
    
    
    
    
    
    }
    
    //进行懒加载空间
    
    private lazy var headImageView: UIImageView =  {
       
        //将imageView的属性都给设置好
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        
        imageView.layer.cornerRadius = 40
        
        imageView.layer.masksToBounds = true
        
        
        //取头像地址添加到头像上去
        
        if let avertImage = CZUserAccountViewModel.sharedViewModel.account?.avatar_large {
            
            //如果有值,就来到这里,进行操作
            
            imageView.sd_setImageWithURL(NSURL(string: avertImage), placeholderImage: UIImage(named: "avatar_default_big"))
            
            
        }
        
        
        
        return imageView
        
        
    }()
    
    //注意懒加载label
    
    private lazy var nameLabel:UILabel = {
       
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        label.text = "欢迎回来"
        
        
        label.alpha = 0.0
        
        return label
        
        
        
    }()
    
    

    
}
